"""
Server端初级聊天
"""

import socket
import threading


def handle_sock(sock, addr):
    # 获取从客户端发送的数据
    while True:
        data = sock.recv(1024)
        print(data.decode("utf8"))

        if "bye" == data.decode("utf8"):
            sock.close()

        re_data = input()
        if re_data:
            sock.send(re_data.encode("utf8"))


def run_server():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(("0.0.0.0", 8090))
    server.listen()

    while True:
        sock, addr = server.accept()
        # 用线程去处理新接收的连接（用户）
        client_thread = threading.Thread(target=handle_sock, args=(sock, addr))
        client_thread.start()


if __name__ == '__main__':
    run_server()
